﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScrumMonster;
using System.IO;

namespace ScrumMonsterConsole
{
    class Program
    {
        private const string INSTANCE_URL = "http://tracker.silverkey.us";
        
        static void Main(string[] args)
        {
            Start();
        }

        static void Start()
        {
            Console.WriteLine("Fogbugz CSV File Generator");
            Console.WriteLine("--------------------------------");

            Console.Write("Enter Fogubugz Username: ");
            string username = Console.ReadLine();

            Console.Write("Enter Fogbugz Password: ");
            string password = Console.ReadLine();

            Console.Write("Enter CSV File Path to save (ex.: c:\\sprintcases.csv):");
            string path = Console.ReadLine();

            Console.WriteLine("Connecting to Fogbugz server tracker.silverkey.us ...");

            FogBugzContext context = new FogBugzContext(INSTANCE_URL, username, password);
            if (context.Logged)
                Console.WriteLine("Authentication Succeeded");
            else
            {
                Console.WriteLine("Authentication Failed. Try Again? (Y/N)");
                if (Console.ReadLine() == "Y")
                {
                    Start();
                }
                else
                {
                    return;
                }
            }
                        
            StreamWriter writer = System.IO.File.CreateText("c:\\sprintcase.csv");

            Console.WriteLine("Retrieving the cases ...");

            foreach (Task task in context.GetTasks("Havana Commerce", "Sprint 5"))
            {
                writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", task.Number, task.Title, task.EmailAssignedTo, task.Status, task.EstimatedTime.TotalHours, task.ElapsedTime.TotalHours));
            }

            writer.Close();

            Console.WriteLine(string.Format("File Created: {0}", path));

        }
    }
}
